<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Pc_News extends Stourweb_Controller{
    /*
     * 总控制器
     * */

    private $typeid = 115;
    private $_cache_key = '';
    public function before()
    {
        parent::before();
        //检查缓存
        $this->_cache_key = Common::get_current_url();
        $html = Common::cache('get',$this->_cache_key);
        $genpage = intval(Arr::get($_GET,'genpage'));
        if(!empty($html) && empty($genpage))
        {
            echo $html;
            exit;
        }
        $channelname = Model_Nav::get_channel_name($this->typeid);
        $this->assign('typeid', $this->typeid);
        $this->assign('channelname', $channelname);
    }

    //首页
    public function action_index()
    {

        $seoinfo = Model_Nav::get_channel_seo($this->typeid);
        $this->assign('seoinfo', $seoinfo);
        $templet = Product::get_use_templet('news_index');
        $templet = $templet ? $templet : 'news/index';
        $this->display($templet);
        //缓存内容
        $content = $this->response->body();
        Common::cache('set',$this->_cache_key,$content);
    }
    //详细页
    public function action_show()
    {
        $aid = intval($this->request->param('aid'));
        $info = Model_News::detail_aid($aid);
        if (!$info)
        {
            $this->request->redirect('error/404');
        }

        //seo
        $seoInfo = Product::seo($info);


        //上一条,下一条
        $info['prev'] = Model_News::get_prev_next('prev',$info['id']);
        $info['next'] = Model_News::get_prev_next('next',$info['id']);

        //扩展字段信息
        $extend_info = Model_News::extend($info['id']);
        $this->assign('seoinfo', $seoInfo);
        $this->assign('info', $info);
        $this->assign('tagword', $info["tagword"]);
        $this->assign('extendinfo',$extend_info);
        ///todo 添加读取模板的功能

        $templet = Model_News::get_show_template($info['id']);
        $templet = $templet ? $templet : 'news/show';
        $this->display($templet);


        //缓存内容
        $content = $this->response->body();
        Common::cache('set',$this->_cache_key,$content);

    }

    //列表页
    public function action_list()
    {
        //参数值获取

        $category_id = $this->request->param('categoryid');
        if(empty($category_id))
        {
            echo 'no categoryId';
            exit();
        }
        $category_info = Model_News_Attr::get_category_info($category_id);
        if($category_info['pid']!=0)
        {
            $parent_category_info = Model_News_Attr::get_category_info($category_info['pid']);
        }
        else
        {
            $parent_category_info = $category_info;
        }

        $p = intval($this->request->param('p'));


        $pagesize = 10;
        $keyword = Common::remove_xss(Arr::get($_GET,'keyword'));
        $keyword = strip_tags($keyword);
        $keyword = St_String::filter_mark($keyword);


        $route_array = array(
            'controller' => $this->request->controller(),
            'action' => $this->request->action(),
            'categoryid' => $category_info['id']
        );


        $out = Model_News::search_result($route_array,$keyword,$p,$pagesize);
        $pager = Pagination::factory(
            array(

                'current_page' => array('source' => 'route', 'key' => 'p'),
                'view'=>'default/pagination/search',
                'total_items' => $out['total'],
                'items_per_page' => $pagesize,
                'first_page_in_url' => false,
            )
        );
        //配置访问地址 当前控制器方法

        $pager->route_params($route_array);



        $seotitle = $category_info['seotitle'] ? $category_info['seotitle'] : $category_info['attrname'];
        $seo_info = array(
            'seotitle' => $seotitle,
            'keyword' => $category_info['keyword'],
            'description' => $category_info['description']
        );

        $page_title = $p>1 ?  '第'.$p.'页-' : '';

        $search_title = $page_title.$seotitle;
        $this->assign('search_title',$search_title);
        $this->assign('list',$out['list']);
        $this->assign('seoinfo',$seo_info);
        $this->assign('param',$route_array);
        $this->assign('currentpage',$p);
        $this->assign('pageinfo',$pager);
        $this->assign('total_count',$out['total']);
        $this->assign('category_info',$category_info);
        $this->assign('parent_category_info',$parent_category_info);

        ///todo 添加读取模板的功能.
        $templet = Model_News::get_list_template($category_info['id']);
        $templet = $templet ? $templet : 'news/list';
        $this->display($templet);
        //缓存内容
        $content = $this->response->body();
        Common::cache('set',$this->_cache_key,$content);
    }


    //更新访问数量
    public function action_ajax_add_shownum()
    {
        $id = intval($_POST['id']);
        if($id)
        {
           Model_News::update_click_num($id);
        }
    }










}